package itcast.dao.impl;

import itcast.dao.NoticeDao;
import itcast.domain.Build;
import itcast.domain.Notice;
import itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class NoticeDaoImpl implements NoticeDao {
    /**
     * 申明JdbcTemplate类,出入连接池
     */
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    //插入公告信息
    @Override
    public void inser_tNotice(Notice notice) {
        String sql1="select buildmanager_id from buildmanager where  buildmanager_name=?";
        int id=template.queryForObject(sql1,Integer.class,notice.getBuildmanager_name());
        System.out.println(id);

        String sql="insert into notice(notice_buildmanagerid,notice_remark,notice_date) values(?,?,?)";
        template.update(sql,id,notice.getNotice_remark(),notice.getNotice_date());

    }
    //找到单个公告

    @Override
    public Notice findNoticeByID(String buildmanager_name) {
        String sql="select notice_date,notice_remark,buildmanager_name " +
                "from notice,buildmanager " +
                "where notice_buildmanagerid=buildmanager_id and buildmanager_name=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Notice>(Notice.class),buildmanager_name);
    }

    //学生查询所有数据

    @Override
    public List<Notice> findAll() {
        //查找公告信息
        //使用jdbc操作数据库
        //1.定义sql
        String sql="select notice_id,notice_buildmanagerid,notice_remark,notice_date,buildmanager_name " +
                "from notice,buildmanager where notice_buildmanagerid=buildmanager_id";
        //query返回list集合，BeanPropertyRowMapper指定字节码
        List<Notice> notices=template.query(sql,new BeanPropertyRowMapper<Notice>(Notice.class));

        return notices;
    }

    //根据公告编号删除公告
    @Override
    public void delNotice_id(int notice_id) {
        System.out.println(notice_id);
        String sql="delete from notice where notice_id=?";
        template.update(sql,notice_id);
    }


}
